package com.yiren.dbaa.module.practice_set.carmodel.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.dbaa.R;
import com.yiren.dbaa.module.practice_set.carmodel.enums.AntennaLocationEnum;
import com.yiren.dbaa.module.practice_set.carmodel.enums.AntennaTypeEnum;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author lwb
 * @date : 2022/12/5 10:18
 * @description: 车模首页适配器
 */
public class CarModelHomeAdapter extends BaseAdapter {

    private Context context;
    private List<CarModelEntity> list;
    public int mSelect = 0;
    private boolean isPartner;

    public CarModelHomeAdapter(Context context, List<CarModelEntity> list) {
        this.context = context;
        this.list = list;
        isPartner = LoadConfigApi.loadUserParamConfig(context).isPartner();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.item_home_carmodel, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        CarModelEntity carModel = list.get(position);
        holder.tvCarName.setText(carModel.getName());
        holder.tvUpdateBtn.setVisibility(View.GONE);
        //可更新
        if (carModel.isUpdate()) {
            holder.tvUpdate.setVisibility(View.VISIBLE);
        } else {
            holder.tvUpdate.setVisibility(View.GONE);
        }
        //未发布的考规
        if (carModel.getCarModelVer() == 0) {
            holder.tvCarVersion.setText("未公开发布，暂无版本");
            holder.tvDelete.setVisibility(View.VISIBLE);
            holder.tvManage.setText("管理");
        } else {
            holder.tvCarVersion.setText("车模版本：V" + carModel.getCarModelVer());
            holder.tvDelete.setVisibility(View.GONE);
        }
        holder.tvAntennaType.setText("天线类型：" + AntennaTypeEnum.getValue(carModel.getAntennaType()));
        holder.tvPositioned.setText("摆放位置：" + AntennaLocationEnum.getValue(carModel.getPositioned()));
        //是否下载
        if (carModel.isDownLoad()) {
            if (isPartner) {
                holder.tvManage.setText("管理");
            } else {
                holder.tvManage.setText("已下载");
            }
        } else {
            holder.tvManage.setText("下载");
        }

        holder.tvUpdateBtn.setOnClickListener(v -> {
            if (mOnUpdateClickListener != null) {
                mOnUpdateClickListener.onUpdateClick(v, position);
            }
        });
        holder.tvDelete.setOnClickListener(v -> {
            if (mOnDeleteClickListener != null) {
                mOnDeleteClickListener.onDeleteClick(v, position);
            }
        });
        holder.tvManage.setOnClickListener(v -> {
            if (mOnManageClickListener != null) {
                mOnManageClickListener.onManageClick(v, position);
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.tv_car_name)
        TextView tvCarName;
        @BindView(R.id.tv_update)
        TextView tvUpdate;
        @BindView(R.id.tv_car_version)
        TextView tvCarVersion;
        @BindView(R.id.tv_antenna_type)
        TextView tvAntennaType;
        @BindView(R.id.tv_positioned)
        TextView tvPositioned;
        @BindView(R.id.tv_update_btn)
        TextView tvUpdateBtn;
        @BindView(R.id.tv_delete)
        TextView tvDelete;
        @BindView(R.id.tv_manage)
        TextView tvManage;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    //更新点击回调
    public interface OnUpdateClickListener {
        void onUpdateClick(View view, int position);
    }

    private OnUpdateClickListener mOnUpdateClickListener;

    public void setOnUpdateClickListener(OnUpdateClickListener onUpdateClickListener) {
        mOnUpdateClickListener = onUpdateClickListener;
    }

    //条目删除回调
    public interface OnDeleteClickListener {
        void onDeleteClick(View view, int position);
    }

    private OnDeleteClickListener mOnDeleteClickListener;

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        mOnDeleteClickListener = onDeleteClickListener;
    }

    //管理点击回调
    public interface OnManageClickListener {
        void onManageClick(View view, int position);
    }

    private OnManageClickListener mOnManageClickListener;

    public void setOnManageClickListener(OnManageClickListener onManageClickListener) {
        mOnManageClickListener = onManageClickListener;
    }

}